﻿function TBSerials() {
}

TBSerials.prototype.ToSerials = function(tableId) {
    var json = "{";
    var cols = $("#" + tableId).find("input[type='text']");
    for (var i = 0; i < cols.length; i++) {
        json += this.TabCol(cols[i]);
    }
    var colsPwd = $("#" + tableId).find("input[type='password']");
    for (var i = 0; i < colsPwd.length; i++) {
        json += this.TabCol(colsPwd[i]);
    } 
    var cols2 = $("#" + tableId).find("select");
    for (var i = 0; i < cols2.length; i++) {
        json += this.TabCol(cols2[i]);
    }

    var cols3 = $("#" + tableId).find("label");
    for (var i = 0; i < cols3.length; i++) {
        json += this.TabColLabel(cols3[i]);
    }

    var cols4 = $("#" + tableId).find("input[type='radio']");
    for (var i = 0; i < cols4.length; i++) {
        json += this.TabColRadion(cols4[i]);
    }

    var cols5 = $("#" + tableId).find("textarea");
    for (var i = 0; i < cols5.length; i++) {
        json += this.TabColLabelField(cols5[i]);
    }

    var cols6 = $("#" + tableId).find("input[type='checkbox']");
    for (var i = 0; i < cols6.length; i++) {
        json += this.TabColRadion(cols6[i]);
    }

    var cols7 = $("#" + tableId).find("input[type='hidden']");
    for (var i = 0; i < cols7.length; i++) {
        json += this.TabCol(cols7[i]);
    }
    json += "}";
    return json;
}
TBSerials.prototype.TabCol = function(col) {

    var fieldname = $(col).attr("fieldname");
    var dt = $(col).attr("dataType");

    if (!fieldname) {
        return "";
    }

    var val = $(col).val();

    if (val == null) {
        val = "";
    }
    else {
        val = val.replace(/\\/g, "\\\\");
    }

    var json = "";
    if (!dt || dt != "num") {
        if (typeof (val) == "string")
            json = "\"" + fieldname + "\":\"" + $.trim(val) + "\"\,";
        else
            json = "\"" + fieldname + "\":\"" + val + "\"\,";
    }
    else {
        if (val == "")
            val = 0;
        json = "\"" + fieldname + "\":" + val + ",";
    }

    return json;
}
TBSerials.prototype.TabColLabel = function(col) {
    var fieldname = $(col).attr("fieldname");

    if (!fieldname) {
        return "";
    }

    var val = $(col).text();

    if (val == null) {
        val = "";
    }

    var json = "";
    if (typeof (val) == "string")
        json = "\"" + fieldname + "\":\"" + escape($.trim(val)) + "\"\,";
    else
        json = "\"" + fieldname + "\":\"" + val + "\"\,";
    return json;
}
TBSerials.prototype.TabColLabelField = function(col) {
    var fieldname = $(col).attr("fieldname");

    if (!fieldname) {
        return "";
    }

    var val = $(col).val();

    if (val == null) {
        val = "";
    }
    var json = "";
    json = "\"" + fieldname + "\":\"" + escape(val) + "\"\,";
    return json;
}
TBSerials.prototype.TabColRadion = function(col) {
    var fieldname = $(col).attr("fieldname");
    var datatype = $(col).attr("datatype");

    if (!fieldname) {
        return "";
    }

    var val = col.checked;

    if (datatype == "int") {
        if (col.checked) {
            val = 1;
        } else {
            val = 0;
        }
    }

    var json = "";
    if (typeof (val) == "string")
        json = "\"" + fieldname + "\":\"" + $.trim(val) + "\"\,";
    else
        json = "\"" + fieldname + "\":\"" + val + "\"\,";
    return json;
}
TBSerials.prototype.JsonToTab = function(tabId, JsonStr) {

    var cols = $("#" + tabId).find("input[type='text']");
    for (var i = 0; i < cols.length; i++) {
        var fieldname = $(cols[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        var val1 = JsonStr[fieldname];
        if (val1 != null) {
            cols[i].value = val1;
        }
    }
    var cols2 = $("#" + tabId).find("select");
    for (var i = 0; i < cols2.length; i++) {
        var fieldname = $(cols2[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        var val2 = JsonStr[fieldname];
        if (val2 != null) {
            cols2[i].value = val2;
        }
    }
    var cols3 = $("#" + tabId).find("label");
    for (var i = 0; i < cols3.length; i++) {
        var fieldname = $(cols3[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        var val3 = JsonStr[fieldname];
        if (val3 != null) {
            cols3[i].innerHTML = val3;
        }
    }
    var cols4 = $("#" + tabId).find("input[type='radio']");
    for (var i = 0; i < cols4.length; i++) {
        var fieldname = $(cols4[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        cols4[i].checked = JsonStr[fieldname];
    }
    var cols5 = $("#" + tabId).find("textarea");
    for (var i = 0; i < cols5.length; i++) {
        var fieldname = $(cols5[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        var val5 = JsonStr[fieldname];
        if (val5 != null) {
            cols5[i].value = unescape(val5);
        }
    }

    var cols6 = $("#" + tabId).find("input[type='checkbox']");
    for (var i = 0; i < cols6.length; i++) {
        var fieldname = $(cols6[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        cols6[i].checked = JsonStr[fieldname];
    }

    var cols7 = $("#" + tabId).find("input[type='hidden']");
    for (var i = 0; i < cols7.length; i++) {
        var fieldname = $(cols7[i]).attr("fieldname");
        if (!fieldname) {
            continue;
        }
        var val7 = JsonStr[fieldname];
        if (val7 != null) {
            cols7[i].value = val7;
        }
    }
}
